<?php
/*
 * Author:  Somnath Shinde- soma.shinde@gmail.com Mobile No:- 9423792564
 * 
 * Create Date: 
 * 
 * Version of MYSQL_to_PHP: 1.1
 * 
 * Ranzani( YZ'S );
 * 
 */
require_once 'Base/BaseAppointments.class.php';

Class Appointments {

	public function  __construct() {
			if( true == isset( $_SERVER['connection'] ) ) {
				$this->connection = $_SERVER['connection'];
			}
	}

	
	/*******************************************************************************************
	******************************** Fetch Functions *******************************************
	*******************************************************************************************/

	public function fetchSql( $strSql ) {
		$result = $this->connection->RunQuery( $strSql );
		$arrobjAppointments = array();
		while( $row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$objAppointments = new BaseAppointments();
			$objAppointments->setId( ( true == isset( $row["id"] ) ) ? $row["id"] : 'NULL' );
			$objAppointments->setPatientId( ( true == isset( $row["patient_id"] ) ) ? $row["patient_id"] : 'NULL' );
			$objAppointments->setDoctorId( ( true == isset( $row["doctor_id"] ) ) ? $row["doctor_id"] : 'NULL' );
			$objAppointments->setDateTime( ( true == isset( $row["date_time"] ) ) ? $row["date_time"] : 'NULL' );
			$objAppointments->setRoomNumber( ( true == isset( $row["room_number"] ) ) ? $row["room_number"] : 'NULL' );
			$objAppointments->setNote( ( true == isset( $row["note"] ) ) ? $row["note"] : 'NULL' );
			$objAppointments->setIsRemainder( ( true == isset( $row["is_remainder"] ) ) ? $row["is_remainder"] : 'NULL' );
			$objAppointments->setOverView( ( true == isset( $row["over_view"] ) ) ? $row["over_view"] : 'NULL' );
			array_push( $arrobjAppointments, $objAppointments );
		}
		return $arrobjAppointments;
	}
	public function fetchAllAppointments() {
		$result = $this->connection->RunQuery("SELECT * FROM APPOINTMENTS");
		$arrobjAppointments = array();
		while( $row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$objAppointments = new BaseAppointments();
			$objAppointments->setId( ( true == isset( $row["id"] ) ) ? $row["id"] : 'NULL' );
			$objAppointments->setPatientId( ( true == isset( $row["patient_id"] ) ) ? $row["patient_id"] : 'NULL' );
			$objAppointments->setDoctorId( ( true == isset( $row["doctor_id"] ) ) ? $row["doctor_id"] : 'NULL' );
			$objAppointments->setDateTime( ( true == isset( $row["date_time"] ) ) ? $row["date_time"] : 'NULL' );
			$objAppointments->setRoomNumber( ( true == isset( $row["room_number"] ) ) ? $row["room_number"] : 'NULL' );
			$objAppointments->setNote( ( true == isset( $row["note"] ) ) ? $row["note"] : 'NULL' );
			$objAppointments->setIsRemainder( ( true == isset( $row["is_remainder"] ) ) ? $row["is_remainder"] : 'NULL' );
			$objAppointments->setOverView( ( true == isset( $row["over_view"] ) ) ? $row["over_view"] : 'NULL' );
			array_push( $arrobjAppointments, $objAppointments);
		}
		return $arrobjAppointments;
	}
	public function fetchAllAppointmentsByIds( $arrintIds ) {
		$result = $this->connection->RunQuery("SELECT * FROM APPOINTMENTS WHERE id IN ( implode( "," $arrintIds ) " );
		$arrobjAppointments = array();
		while( $row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$objAppointments->setId( ( true == isset( $row["id"] ) ) ? $row["id"] : 'NULL' );
			$objAppointments->setPatientId( ( true == isset( $row["patient_id"] ) ) ? $row["patient_id"] : 'NULL' );
			$objAppointments->setDoctorId( ( true == isset( $row["doctor_id"] ) ) ? $row["doctor_id"] : 'NULL' );
			$objAppointments->setDateTime( ( true == isset( $row["date_time"] ) ) ? $row["date_time"] : 'NULL' );
			$objAppointments->setRoomNumber( ( true == isset( $row["room_number"] ) ) ? $row["room_number"] : 'NULL' );
			$objAppointments->setNote( ( true == isset( $row["note"] ) ) ? $row["note"] : 'NULL' );
			$objAppointments->setIsRemainder( ( true == isset( $row["is_remainder"] ) ) ? $row["is_remainder"] : 'NULL' );
			$objAppointments->setOverView( ( true == isset( $row["over_view"] ) ) ? $row["over_view"] : 'NULL' );
			array_push( $arrobjAppointments, $objAppointments );
		}
		return $arrobjAppointments;
	}

	public function fetchAppointmentsById( $intId ) {
		$result = $this->connection->RunQuery( "SELECT * FROM APPOINTMENTS WHERE id = " . $intId );
			$objAppointments = NULL;
		while($row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$objAppointments = new BaseAppointments();
			$objAppointments->setId( ( true == isset( $row["id"] ) ) ? $row["id"] : 'NULL' );
			$objAppointments->setPatientId( ( true == isset( $row["patient_id"] ) ) ? $row["patient_id"] : 'NULL' );
			$objAppointments->setDoctorId( ( true == isset( $row["doctor_id"] ) ) ? $row["doctor_id"] : 'NULL' );
			$objAppointments->setDateTime( ( true == isset( $row["date_time"] ) ) ? $row["date_time"] : 'NULL' );
			$objAppointments->setRoomNumber( ( true == isset( $row["room_number"] ) ) ? $row["room_number"] : 'NULL' );
			$objAppointments->setNote( ( true == isset( $row["note"] ) ) ? $row["note"] : 'NULL' );
			$objAppointments->setIsRemainder( ( true == isset( $row["is_remainder"] ) ) ? $row["is_remainder"] : 'NULL' );
			$objAppointments->setOverView( ( true == isset( $row["over_view"] ) ) ? $row["over_view"] : 'NULL' );
		}
		return $objAppointments;
	}

	public function GetKeysOrderBy($column, $order){
		$keys = array(); $i = 0;
		$result = $this->connection->RunQuery("SELECT id from appointments order by $column $order");
			while($row = $result->fetch_array(MYSQLI_ASSOC)){
				$keys[$i] = $row["id"];
				$i++;
			}
	return $keys;
	}

	public function endAppointments(){
		$this->connection->CloseMysql();
	}

}